|
1
|
|
|
/** |
|
2
|
|
|
* hsBot: Supply template and your bot is ready. |
|
3
|
|
|
* |
|
4
|
|
|
* Hardik Shah <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* MIT License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
'use strict'; |
|
10
|
|
|
|
|
11
|
|
|
function ChatDB(topicList, topics){ |
|
12
|
|
|
this.topicList = topicList; |
|
13
|
|
|
this.topics = topics; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
ChatDB.prototype._findTopicByCommand = function(command){ |
|
17
|
|
|
var topic; |
|
18
|
|
|
this.topicList.some(function(value){ |
|
19
|
|
|
if(value.command && command && value.command.indexOf(command.toLowerCase()) > -1){ |
|
20
|
|
|
return topic = value.topic; |
|
21
|
|
|
} else{ |
|
22
|
|
|
return false; |
|
23
|
|
|
} |
|
24
|
|
|
}); |
|
25
|
|
|
return topic; |
|
26
|
|
|
}; |
|
27
|
|
|
|
|
28
|
|
|
ChatDB.prototype.getTopicFlow = function(topicName){ |
|
29
|
|
|
var topicFlow; |
|
30
|
|
|
this.topics.some(function(value){ |
|
31
|
|
|
if(value.topic === topicName){ |
|
32
|
|
|
return topicFlow = value.flow; |
|
33
|
|
|
} else { |
|
34
|
|
|
return false; |
|
35
|
|
|
} |
|
36
|
|
|
}); |
|
37
|
|
|
return topicFlow; |
|
38
|
|
|
}; |
|
39
|
|
|
|
|
40
|
|
|
ChatDB.prototype.getTemplateObj = function(userName, topicFlow, pattern, preQ) { |
|
41
|
|
|
var templateObj; |
|
42
|
|
|
if(pattern){ |
|
43
|
|
|
topicFlow.some(function(obj){ |
|
44
|
|
|
if((obj.pattern && (obj.pattern.toLowerCase() === pattern.toLowerCase() || obj.pattern === "*")) && |
|
45
|
|
|
(preQ ? (obj.preQ ? preQ.indexOf(obj.preQ) > -1 : true) : true)){ |
|
46
|
|
|
return templateObj = obj; |
|
47
|
|
|
} else { |
|
48
|
|
|
return false; |
|
49
|
|
|
} |
|
50
|
|
|
}); |
|
51
|
|
|
} else if(userName) { |
|
52
|
|
|
topicFlow.some(function(obj){ |
|
53
|
|
|
if(obj.redirect === true){ |
|
54
|
|
|
return templateObj = obj; |
|
55
|
|
|
} else { |
|
56
|
|
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
}); |
|
59
|
|
|
} else { |
|
60
|
|
|
topicFlow.some(function(obj){ |
|
61
|
|
|
if(obj.default === true){ |
|
62
|
|
|
return templateObj = obj; |
|
63
|
|
|
} else { |
|
64
|
|
|
return false; |
|
65
|
|
|
} |
|
66
|
|
|
}); |
|
67
|
|
|
} |
|
68
|
|
|
return templateObj; |
|
69
|
|
|
}; |
|
70
|
|
|
|
|
71
|
|
|
ChatDB.prototype._findDefaultTopic = function(){ |
|
72
|
|
|
var topic; |
|
73
|
|
|
this.topicList.some(function(value){ |
|
74
|
|
|
if(value.default === true){ |
|
75
|
|
|
return topic = value.topic; |
|
76
|
|
|
} else { |
|
77
|
|
|
return false; |
|
78
|
|
|
} |
|
79
|
|
|
}); |
|
80
|
|
|
return topic; |
|
81
|
|
|
}; |
|
82
|
|
|
|
|
83
|
|
|
ChatDB.prototype.getDefaultMessage = function(){ |
|
84
|
|
|
var defaultMessage; |
|
85
|
|
|
this.topicList.some(function(value){ |
|
86
|
|
|
if(value.hasOwnProperty("default_message")){ |
|
87
|
|
|
return defaultMessage = value.default_message; |
|
88
|
|
|
} else { |
|
89
|
|
|
return false; |
|
90
|
|
|
} |
|
91
|
|
|
}); |
|
92
|
|
|
return defaultMessage; |
|
93
|
|
|
}; |
|
94
|
|
|
|
|
95
|
|
|
module.exports = ChatDB; |